package com.yanu.t2s.controller;


import com.yanu.t2s.entity.Pig;
import com.yanu.t2s.entity.User;
import com.yanu.t2s.service.PigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/pig")
public class PigController {
    @Autowired
    PigService pigService;

    //属于某个养殖户的所有的猪
    @GetMapping("getyangzhi/{iduser}")
    public List<Pig> findByIDyangzhi(@PathVariable int iduser){
        return pigService.findByIDyangzhi(iduser);
    }

    //属于某个屠宰场的所有的猪
    @GetMapping("gettuzai/{iduser}")
    public List<Pig> findByIDtuzai(@PathVariable int iduser){
        return pigService.findBYIDtuzai(iduser);
    }

    //所有的猪，废弃，无意义
    @GetMapping("get/all")
    public List<Pig> findAll(){
        System.out.print("find all");
       return pigService.findAllPig();
       //return null;
    }
    //插入一条新的猪
    @RequestMapping(value = "insert",method={RequestMethod.POST, RequestMethod.GET},produces = "application/json;charset=UTF-8")
    String insert( Pig pig) {
            pigService.insertpig(pig);
            return "chenggong";
    }



    //购买一头猪（事务管理）
    @Transactional()
    @RequestMapping(value = "buyonepig",method={RequestMethod.POST, RequestMethod.GET},produces = "application/json;charset=UTF-8")
    void buyonepig(@RequestParam int pigid,@RequestParam int userid,@RequestParam double weight){
        pigService.buyonepig1(pigid,userid,weight);
        pigService.buyonepig2(pigid,userid,weight);
    }

}
